package com.che.software.testato.business;

import java.util.List;

import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.springframework.stereotype.Service;

import com.che.software.testato.domain.entity.MatrixResult;
import com.che.software.testato.util.jfreechart.LineChartGraphistUtil;

/**
 * Manager dedicated to the selectives charts.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @since August, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Service("selectiveChartManager")
public class SelectiveChartManager {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(SelectiveChartManager.class);

	/**
	 * Creates a selective chart from a set of scripts related to 2 criterions.
	 * Excluded values are optionals and will be created as a different set of
	 * points. This allow to colored them in red in following steps.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param xValues the x axis values list.
	 * @param yValues the y axis values list.
	 * @param title the title to give to the line chart.
	 * @param xLabel the criterion label to display on the x axis.
	 * @param yLabel the criterion label to display on the y axis.
	 * @param xExcludedValues the x axis excluded values.
	 * @param yExcludedValues the y axis excluded values.
	 * @return the resulting chart.
	 * @since August, 2011.
	 */
	public JFreeChart createSelectiveChart(List<MatrixResult> xValues, List<MatrixResult> yValues, String title, String xLabel, String yLabel, List<MatrixResult> xExcludedValues, List<MatrixResult> yExcludedValues) {
		LOGGER.debug("createSelectiveChart(" + xLabel + "," + yLabel + "," + xValues.size() + "," + yValues.size() + ").");
		XYSeries notExcludedData = new XYSeries("1"), excludedData = new XYSeries("2"), lowMediumLine = new XYSeries("3"), mediumHighLine = new XYSeries("4");
		for (int i = 0; i < xValues.size(); i++) {
			notExcludedData.add(yValues.get(i).getPercentage() * 100, xValues.get(i).getPercentage() * 100);
		}
		if (null != xExcludedValues) {
			for (int j = 0; j < xExcludedValues.size(); j++) {
				excludedData.add(yExcludedValues.get(j).getPercentage() * 100, xExcludedValues.get(j).getPercentage() * 100);
			}
		}
		double maxAbscissaValue = LineChartGraphistUtil.getMaxAbscissaValue(xValues, xExcludedValues) + 2;
		lowMediumLine.add(0, 0);
		lowMediumLine.add(0.5 * maxAbscissaValue, maxAbscissaValue);
		mediumHighLine.add(0, 0);
		mediumHighLine.add(1.5 * maxAbscissaValue, maxAbscissaValue);
		XYSeriesCollection dataSet = new XYSeriesCollection();
		dataSet.addSeries(notExcludedData);
		dataSet.addSeries(excludedData);
		dataSet.addSeries(lowMediumLine);
		dataSet.addSeries(mediumHighLine);
		return ChartFactory.createXYLineChart(title, xLabel, yLabel, dataSet, PlotOrientation.HORIZONTAL, false, true, false);
	}

}